package com.yaodian100.ec.product.batch;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;

import com.yaodian100.core.admin.entity.Role;
import com.yaodian100.core.admin.entity.User;
import com.yaodian100.core.admin.service.UserService;
import com.yaodian100.core.admin.utils.AdminHelper;
import com.yaodian100.core.mail.entity.Mail;
import com.yaodian100.core.mail.service.MailService;
import com.yaodian100.core.template.service.TemplateService;
import com.yaodian100.core.thread.batch.AbstractRunJob;
import com.yaodian100.ec.category.entity.Category;
import com.yaodian100.ec.common.EnvParameters;

public class BuildProductJob extends AbstractRunJob {
	private List<Map<String, Object>> products;
	private UserService userService;
	private TemplateService velocityService;
	private EnvParameters envParameters;
	private MailService mailService;
	private String rootId;
	private List<Category> rootCategories;

	public BuildProductJob(List<Map<String, Object>> products, String rootId, List<Category> rootCategories) {
		super("BuildProductJob");
		this.products = products;
		this.rootId = rootId;
		this.rootCategories = rootCategories;
	}

	@Override
	public void execute() {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat dateFormatFile = new SimpleDateFormat("yyyyMMddHH");
		String filePath = "/yaodian100.com/eai/checkproduct/" + dateFormat.format(calendar.getTime()) + "/"
				+ dateFormatFile.format(calendar.getTime()) + "_" + rootId + ".xls";

		File fileObj = new File("/yaodian100.com/eai/checkproduct/" + dateFormat.format(calendar.getTime()) + "/");
		if (!fileObj.exists()) {
			// 判斷目碌是否存在,没有的話建立一個新的
			fileObj.mkdirs();
		}
		File checkfile = new File(filePath);
		if (checkfile.exists()) {
			// 判斷檔案是否存在,有的話刪除
			checkfile.delete();
		}
		try {
			Map<String, Object> excelContent = new HashMap<String, Object>();
			excelContent.put("products", products);
			excelContent.put("user", AdminHelper.getUser());
			excelContent.put("sizecount", products.size() + 1);
			// checkProduct.vm 是为了建excel使用
			String fileContent = velocityService.format("checkProduct", excelContent);
			FileUtils.writeStringToFile(checkfile, fileContent);
		} catch (Exception e) {
			logger.error("写档错误", e);
		}

		try {
			// 寄信给各馆的pm
			sendMail(rootId, filePath);
		} catch (Exception e) {
			logger.error(rootId + "馆寄信失败", e);
		}
	}

	private void sendMail(String rootId, String filePath) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH");
		SimpleDateFormat dateFormatFile = new SimpleDateFormat("yyyyMMddHH");
		List<User> users = userService.getByRole(rootId, Role.Type.CATEGORY);
		Map<String, Object> contentMap = new HashMap<String, Object>();
		contentMap.put("rootId", rootId);
		contentMap.put("rootCategories", rootCategories);
		contentMap.put("date", dateFormat.format(calendar.getTime()));
		// checkProduHtm.vm是mail的内容
		Mail mail = velocityService.formatToMail("checkProduHtm", contentMap);
		// 加附件
		mail.getAttachments().put(dateFormatFile.format(calendar.getTime()) + "_" + rootId + "_safe_inventory.xls",
				filePath);
		mail.setFrom(envParameters.getEmailFromSystem());
		if (users != null) {
			for (User u : users) {
				mail.addTo(u.getUsername(), u.getEmail());
			}
		}
		mailService.save(mail);
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setVelocityService(TemplateService velocityService) {
		this.velocityService = velocityService;
	}

	public void setEnvParameters(EnvParameters envParameters) {
		this.envParameters = envParameters;
	}

	public void setMailService(MailService mailService) {
		this.mailService = mailService;
	}

}
